﻿// Copyright richyp 2009
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Richyp.Bodger.UI {
    public partial class NewListForm : Form {
        public NewListForm() {
            InitializeComponent();
            this.PopulateList(Application.StartupPath + "/data/codex");
        }

        public void PopulateList(string dataPath) {
            CodexParser parser = new CodexParser();
            this.dataGridViewCodex.Rows.Clear();
            List<string> categories = new List<string>();
            Dictionary<string, List<CodexFile>> codexes = new Dictionary<string, List<CodexFile>>();
            foreach (string folder in Directory.GetDirectories(dataPath)) {
                if (File.Exists(folder + "/index.xml")) {
                    CodexFile fileHeader = (CodexFile)parser.Parse(folder, true);
                    if (!categories.Contains(fileHeader.System)) {
                        categories.Add(fileHeader.System);
                        codexes[fileHeader.System] = new List<CodexFile>();
                    }
                    codexes[fileHeader.System].Add(fileHeader);
                }
            }

            categories.Sort();
            foreach (string system in categories) {
                CategoryRow row = new CategoryRow();
                row.Category = system;
                this.dataGridViewCodex.Rows.Add(row);
                List<CodexFile> list = codexes[system];
                List<string> sorted = new List<string>();
                foreach (CodexFile file in list) {
                    sorted.Add(file.Name);
                }
                sorted.Sort();

                foreach (string name in sorted) {
                    foreach (CodexFile file in list) {
                        if (file.Name == name) {
                            DataGridViewRow codexRow = new DataGridViewRow();
                            NiceRenderer nr = new NiceRenderer(file.Name);
                            codexRow.Tag = file;
                            codexRow.Cells.Add(nr);
                            this.dataGridViewCodex.Rows.Add(codexRow);
                        }
                    }
                }
            }
        }

        private CodexFile selectedCodex = null;
        public CodexFile SelectedCodex {
            get {
                return this.selectedCodex;
            }
        }

        public string ListName {
            get {
                return this.textBoxListName.Text;
            }
        }
        public int ListSize {
            get { 
                return (int)this.numericUpDownSize.Value; 
            }
        }

        private void dataGridViewCodex_SelectionChanged(object sender, EventArgs e) {
            if (this.dataGridViewCodex.SelectedRows.Count > 0) {
                this.listBoxSpecialRules.Items.Clear();
                this.listBoxSpecialRules.Text = "";
                this.labelRuleDescription.Text = "";
                if (!(this.dataGridViewCodex.SelectedRows[0] is CategoryRow)) {
                    CodexFile file = (CodexFile)this.dataGridViewCodex.SelectedRows[0].Tag;
                    this.pictureBoxLogo.Image = file.GameSystem.Icon;
                    this.labelName.Text = file.Name;
                    this.labelVersion.Text = "Version: " + file.Version;
                    this.selectedCodex = file;
                    this.buttonOk.Enabled = true;
                    Dictionary<string, string> rules = new Dictionary<string, string>();

                    foreach (SpecialRule rule in file.SpecialRules) {
                        if (!rules.ContainsKey(rule.Name)) {
                            rules.Add(rule.Name, rule.Description);
                        }
                    }
                    foreach (string rule in rules.Keys) {
                        SpecialRule ruleS = new SpecialRule();
                        ruleS.Name = rule;
                        ruleS.Description = rules[rule];
                        this.listBoxSpecialRules.Items.Add(ruleS);
                    }
                } else {
                    this.buttonOk.Enabled = false;
                    this.labelName.Text = "";
                    this.labelVersion.Text = "";
                    this.pictureBoxLogo.Image = null;
                    this.selectedCodex = null;
                }
            } else {
                this.buttonOk.Enabled = false;
                this.labelName.Text = "";
                this.labelVersion.Text = "";
                this.pictureBoxLogo.Image = null;
                this.selectedCodex = null;
            }
        }

        private void dataGridViewCodex_CellContentClick(object sender, DataGridViewCellEventArgs e) {

        }

        private void listBoxSpecialRules_SelectedIndexChanged(object sender, EventArgs e) {
            if (this.listBoxSpecialRules.SelectedItem != null) {
                this.labelRuleDescription.Text =
                    ((SpecialRule)this.listBoxSpecialRules.SelectedItem).Description;
            } else {
                this.labelRuleDescription.Text = "";
            }
        }
    }
}
